package com.yaozheng.vocationaltraining.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.TextView;

import com.yaozheng.vocationaltraining.R;

/**
 * Created by moon on 2017/11/19.
 */

public class ProgressTextView extends android.support.v7.widget.AppCompatTextView {
    private  final static int  DEFAULT_DIRECTION = 1;

    private TextPaint mPaint;
    private Context context;

    private int direction = DEFAULT_DIRECTION;

    private int rate = 0;

    public ProgressTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs);
        this.context = context;
        initView();

        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.ProgressTextView, defStyle, 0);

        direction = a.getInteger(R.styleable.ProgressTextView_direction,DEFAULT_DIRECTION);

    }




    private void initView() {
        mPaint = new TextPaint(getPaint());
        mPaint.setStyle(TextPaint.Style.FILL);//空心和实心样式
        mPaint.setColor(context.getResources().getColor(R.color.lottery_bg_color));
       // mPaint.setColor(Color.RED);
    }

    public void setDirection(int direction){
        this.direction = direction;
        invalidate();
    }

    public void setRate(int rate){
        if(rate <= 0){
            this.rate = 0;
        }else{
            this.rate = rate;
        }

        invalidate();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        if (direction == 1){
            canvas.drawRect(new Rect(0, 0,
                    (int)(getMeasuredWidth() * rate / 10000.0), getMeasuredHeight()), mPaint);
        }else {
            canvas.drawRect(new Rect(getMeasuredWidth() - (int)(getMeasuredWidth() * rate / 10000.0)
                    , 0,
                    getMeasuredWidth(), getMeasuredHeight()), mPaint);
        }
        super.onDraw(canvas);
    }

}
